//
// Created by Administrator on 2019/12/17.
//
#include <iostream>
#include <string.h>
#include <ctime>
#include "math.h"
#include <stdio.h>


using namespace std;


void myswap(int &a, int &b) {
    int temp = a;
    a = b;
    b = temp;
    cout << "我是普通函数 -- myswap(int &a, int &b)" << endl;
}

void myswap(char &a, char &b) {
    char temp = a;
    a = b;
    b = temp;

    cout << "我是普通函数 -- myswap(char &a, char &b)" << endl;
}

template<typename T>
void myswap(T &a, T &b) {
    T temp = a;
    a = b;
    b = temp;
    cout << "我是模板函数 -- myswap(T &a, T &b)" << endl;
}

// 排序模板函数用法案例
template<typename T, typename T2>
int mySort(T *array, T2 size) {
    cout << "array -- " << array << "--size--" << size << endl;
    if (array == NULL) {
        return -1;
    }
    for (T2 i = 0; i < size; ++i) {
        for (T2 j = i + 1; j < size; ++j) {
            if (array[i] > array[j]) {
                T temp = array[j];
                array[j] = array[i];
                array[i] = temp;
            }

        }
    }

    return 0;
}

template<typename T, typename T2>
void printArray(T *array, T2 size) {
    cout << "打印array数组如下:";
    for (T2 i = 0; i < size; ++i) {
        if (i == size - 1) {
            cout << array[i];
        } else {
            cout << array[i] << ", ";
        }
    }
}


// 模板函数使用
void print10() {
    cout << "执行程序--print10()" << endl;
    int a = 10, b = 20;
    myswap(a, b);
    cout << "a -- " << a << "-- b --" << b << endl;

    myswap<>(a, b);  //显示调用模板函数

    float f1 = 303.5, f2 = 1.555;
    myswap(f1, f2);
    cout << "f1 -- " << f1 << "-- f2 --" << f2 << endl;


    int array1[] = {10, 30, 223, 11, 5, 5, 12, 6, 78, 10};
    int size = sizeof(array1) / sizeof(array1[0]);
    mySort<int, int>(array1, size);

    printArray<int, int>(array1, size);

    cout << endl;


}


// 时间类
void print11() {
    cout << "执行程序--print11()" << endl;
    // 基于当前系统的当前日期/时间
    time_t t = time(0);
    char *dt = ctime(&t);

    cout << t << endl;
    cout << dt << endl;
}

/**
 * 位运算符
 */
void print12() {
    cout << "执行程序--print12()-----------------------" << endl;
    int a = 0b1100;     // 二进制表示方法
    int b = 01100;
    int c = 0x1100;

    cout << "二进制a：" << a << ", 八进制b：" << b << ", 十六进制c：" << c << endl;

    char buf[50];
    // itoa 在mac平台下不支持
//    itoa(a, buf, 2);    // 显示二进制
//    printf("打印二进制a->%s \n", buf);
//    itoa(a >> 2, buf, 2);    // 向右位移2两位
//    printf("打印二进制a->%s \n", buf);
//    itoa(a << 2, buf, 2);    // 向左位移2两位
//    printf("打印二进制a->%s \n", buf);

    printf("3 & 5 = %d \n", 3 & 5);
    printf("3 | 5 = %d \n", 3 | 5);
    printf("3 ^ 5 = %d \n", 3 ^ 5);     // 异或^，位数相同时为0，不同时为1
    printf("3的补码为~3:%d\n", ~3);
    printf("5的补码为~5:%d\n", ~5);

    int maxInt = pow(2, 31) - 1;
    printf("int最大值，二进制表示为：-->%s, 十进制表示为-->%d, char长度-->%d \n", buf, maxInt, strlen(buf));

    int f = 6;
    cout << "为运算法~f:" << ~f << endl;

    int g2 = 0b11111111111111111111111111101110;
    cout << "g2二进制变十进制后为:" << g2 << endl;


}

// 随便打印下数据长度
void print13() {
    cout << "执行程序--print13()-----------------------" << endl;

    string str = "1111111111111111111111111111111";
    string str2 = "10000000000000000000000000000000";
    cout << str.length() << endl;
    cout << str2.length() << endl;
}

// 做些循环测试
void print14() {
    cout << "执行程序--print14()-----------------------" << endl;
    int index = 0;
    do {
        index++;
        cout << "do-while循环 : index->" << index << endl;
    } while (index < 10);

    while (index > 0) {
        index--;
        cout << "while- 循环 : index->" << index << endl;
    }
}

const int MAX_ARRAY = 3;

// 数组，指针
void print15() {
    cout << "执行程序--print15()-----------------------" << endl;
    int index = 5;

    cout << "index地址为：" << &index << endl;


    cout << sizeof(char) << endl;
    cout << sizeof(short) << endl;
    cout << sizeof(int) << endl;
    cout << sizeof(float) << endl;
    cout << sizeof(long long) << endl;
    cout << sizeof(double) << endl;
    cout << 46.0 / 2200 << endl;

    int var = 20;   //int数值
    int *p;  // var的指针声明
    p = &var;
    cout << "var类型指针的值为：" << var << ", var取地址即&var:" << &var << ", 打印指针即p:" << p << ", 打印指针指向的地址及*p:" << *p
         << ", 再试试&p为多少:" << &p << endl;
    cout << "p * 5 = " << *p * 5 << endl;
    *p = *p + 1;
    cout << "*p + 1 之后： p * 5 = " << *p * 5 << endl;
    ++*p;
    cout << "++*p 之后： p * 5 = " << *p * 5 << endl;
    *p++;
    cout << "*p++ 之后： p * 5 = " << *p * 5 << "p指针地址为:" << p << endl;
    p++;
    cout << "p++ 之后： p * 5 = " << *p * 5 << "p指针地址为:" << p << endl;
    var++;
    cout << "var++ 之后： p * 5 = " << *p * 5 << endl;

    // NULL指针测试
    int *pstr = NULL;
    cout << "pstr的指针地址为" << pstr << endl;
    if (pstr) {
        cout << "if(pstr)为真的情况" << endl;
    } else {
        cout << "if(pstr)为假的情况" << endl;
    }

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关1<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    int arr[MAX_ARRAY] = {10, 20, 30};
    int *pa;
    pa = arr;
    int i = 0;
    while (pa <= &arr[MAX_ARRAY - 1]) {
        cout << "arr[" << i << "] address is " << pa << endl;
        cout << "arr[" << i << "] value is " << *pa << endl;
        pa++;
        i++;
    }
    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关2<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    for (int index = 0; index < MAX_ARRAY; index++) {
        *(arr + index) = *(arr + index) + 1;
    }
    for (int index = 0; index < MAX_ARRAY; index++) {
        cout << "arr[i" << index << "] = " << *(arr + index) << endl;
    }

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关3<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    for (int j = 0; j < MAX_ARRAY; ++j) {
        *arr = j;
    }
    printArray(arr, MAX_ARRAY);

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关4<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    const char *names[4] = {
            "Zara Ali",
            "Hina Ali",
            "Nuha Ali",
            "Sara Ali",
    };

    for (int k = 0; k < 4; ++k) {
        cout << "Value of names[" << k << "] = ";
        cout << *names[k] << endl;
    }

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关5<<<<<<<<<<<<<<<<<<<<<<<<" << endl;

}

template <typename T, typename T2>
void printValue(T *t, T2 *t2) {
    cout << t << "=" << t2 << endl;
}

/**
 * 返回一个指针
 * @return
 */
int * getPointInt() {
    int c = 333;
    int *pC = &c;
    return pC;
}

// 指针
void print16() {
    cout << "执行程序--print16()-----------------------" << endl;
    cout << "预测，当打印的指针为int 或者 char之类的时候，则直接回显示地址，当如果是char[]数组时，会直接打印数组内容" << endl;
    int intValue = 100;
    char charValue = 'h';
    char charArrayValue[7] =  { 'x' ,'i', 'a', 'r', 'e', 'n', '\0' };
    char charArrayValue2[] = "xiaren2";
    int intArrayValue[5] = {10, 20, 30 ,40};
    int *pInt = &intValue;
    char *pChar = &charValue;
    char *pCharArray = charArrayValue;
    int *pIntArray = intArrayValue;

    cout << "以下是打印" << endl;
    printValue("*pInt", pInt);
    printValue("*pChar", pChar);
    printValue("*pCharArray", pCharArray);
    printValue("*pIntArray", pIntArray);
    printValue("*charArrayValue2", charArrayValue2);


    cout << "其他测试--->" << "=" << pCharArray << endl;
    cout << "其他测试--->" << "=" << *pCharArray << endl;

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<数组相关1指针<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    int iValue = 20;
    int *intArr = &iValue;
    cout << "intArr=" << *intArr << ", intArr地址->" << intArr << ", 另类表示int指针值:" << intArr[0] << endl;

    cout << "xxx=" << getPointInt() << "， int指针值为:" << *getPointInt() << endl;

    cout << "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<引用<<<<<<<<<<<<<<<<<<<<<<<<<<" << endl;
    // 声明简单的变量
    int    i;

    // 声明引用变量
    int&    r = i;

    i = 5;
    cout << "Value of i : " << i << endl;
    cout << "Value of i reference : " << r  << endl;
    cout << "Value of i reference : " << r << ",访问地址试试:" << &r << ",访问i地址为：" << &i << endl;


}

// 模仿dao、mapper哪些功能
template<typename T, class TC, typename ID>
class BaseDao {

    void insert(T t);

    void update(T t);

    T findById(ID id);
};



